unit Map;

interface

uses
  System.Xml, System.Collections, Block, PlayerBlock, Settings, Backround,
  Border;

type
  TMap = class
  private
    _level : Integer;
    _name : String;
    _blocks : ArrayList;
    _borders : ArrayList;
    _player : TPlayerBlock;
    _file : String;
    
    function GetTagValue(xdoc : XmlDocument; tagName : String) : String;
    procedure LoadBlocks(xdoc : XmlDocument);
    procedure LoadBorders(xdoc : XmlDocument);
    procedure AddBlock(blockNode : XmlNode);
    procedure LoadPlayerBlock(xdoc : XmlDocument);
    procedure LoadBackround(Top, Left, Widht, height : integer);
  public
    property Level : Integer read _level write _level;
    property Name : String read _name write _name;
    property Blocks : ArrayList read _blocks;
    property Borders : ArrayList read _borders;
    property PlayerBlock : TPlayerBlock read _player;

    constructor Create(mapFile : String);
    procedure Reload();
  end;

implementation

constructor TMap.Create(mapFile : String);
begin
  inherited Create;

  self._file := mapFile;
  self.Reload();
end;

Procedure Tmap.LoadBackround(Top, Left, Widht, height : integer);
var backround : TBackround;
begin
  top := 30;
  left := 30;
  widht := 30;
  height := 30;
  backround := TBackround.Create(top, left, widht, height);
end;

procedure TMap.Reload();
var
  xdoc : XmlDocument;
  backround : TBackround;

begin
  backround := TBackround.Create(10, 10, 10, 10);
  self._blocks := ArrayList.Create();
  self._borders := ArrayList.Create();

  xdoc := XmlDocument.Create();
  xdoc.Load(self._file);

  self.Level := Int32.Parse(self.GetTagValue(xdoc, 'Level'));
  self.Name := self.GetTagValue(xdoc, 'Name');

  self.LoadBlocks(xdoc);
  self.LoadBorders(xdoc);
  self.LoadPlayerBlock(xdoc);
end;


procedure TMap.LoadBorders(xdoc : XmlDocument);
var
  bordersNode : XmlNode;
  borderNode : XmlNode;
  i : Integer;
  border : TBorder;
  backround : TBackround;

begin
  bordersNode := xdoc.GetElementsByTagName('Borders')[0];

  backround := TBackround.Create(40, 40, 40, 40);

  for i := 0 to bordersNode.ChildNodes.Count - 1 do
  begin
    borderNode := bordersNode.ChildNodes[i];

    border := TBorder.Create(
      borderNode.Attributes['Direction'].Value,
      Int32.Parse(borderNode.Attributes['Top'].Value),
      Int32.Parse(borderNode.Attributes['Left'].Value),
      Int32.Parse(borderNode.Attributes['Width'].Value),
      Int32.Parse(borderNode.Attributes['Height'].Value)
    );
    
    self._borders.Add(border);
  end;
end;

procedure TMap.LoadPlayerBlock(xdoc: XmlDocument);
var
  block : TPlayerBlock;
  blockNode : XmlNode;
  
begin
  blockNode := xdoc.GetElementsByTagName('Player')[0];
  block := TPlayerBlock.Create(
    Int32.Parse(blockNode.Attributes['Width'].Value),
    Int32.Parse(blockNode.Attributes['Height'].Value)
  );

  block.Left := TSettings.BlockWidth * Int32.Parse(blockNode.Attributes['Left'].Value);
  block.Top := TSettings.BlockHeight * Int32.Parse(blockNode.Attributes['Top'].Value);
  block.MoveDirection := blockNode.Attributes['MoveDirection'].Value;

  self._player := block;
end;

procedure TMap.AddBlock(blockNode: XmlNode);
var
  block : TBlock;
  backround : Tbackround;
begin
  backround := TBackround.Create(40, 40, 40, 40);
  block := TBlock.Create(
    Int32.Parse(blockNode.Attributes['Width'].Value),
    Int32.Parse(blockNode.Attributes['Height'].Value)
  );

  block.Left := TSettings.BlockWidth * Int32.Parse(blockNode.Attributes['Left'].Value);
  block.Top := TSettings.BlockHeight * Int32.Parse(blockNode.Attributes['Top'].Value);
  block.MoveDirection := blockNode.Attributes['MoveDirection'].Value;

  self._blocks.Add(block);
end;

procedure TMap.LoadBlocks(xdoc : XmlDocument);
var
  backround : Tbackround;
  blocksNode : XmlNode;
  blockNode : XmlNode;
  i: Integer;
  
begin
  blocksNode := xdoc.GetElementsByTagName('Blocks')[0];
  backround := TBackround.Create(30, 30, 30, 30);
  for i := 0 to blocksNode.ChildNodes.Count - 1 do
  begin
    blockNode := blocksNode.ChildNodes[i];

    self.AddBlock(blockNode);
  end;
end;

function TMap.GetTagValue(xdoc: XmlDocument; tagName: string) : String;
var
  node : XmlNode;

begin
  node := xdoc.GetElementsByTagName(tagName)[0];
  result := node.FirstChild.Value;
end;

end.
